package clientdcsinstance.server.operations.impl;

import java.io.Serializable;

import clientdcsinstance.pds.uchiha.corba.manager.CorbaManager;
import clientdcsinstance.server.datas.DataObject;
import clientdcsinstance.server.operations.Operation;
import clientdcsinstance.server.operations.OperationType;
import clientdcsinstance.server.operations.Replicated;



public class OperationFactory {
	/**
	 * construction de l'opération
	 * @param target : nom du topic cible
	 * @param type : create|delete|update
	 * @param rep : est ou n'es pas repliqué
	 * @param obj : la donnée
	 * @return : l'opération construite
	 */
	public Operation buildOperation(String target, OperationType type, Replicated rep, Serializable obj){
		if(target.equals("topic")){
			if(type.value() == OperationType._create){
				Operation operation = new Operation();
				operation.target = "topic";
				operation.type = OperationType.create;
				operation.replicated = rep;
				operation.data = new DataObject(CorbaManager.getInstance().createAny());
				operation.data.data.insert_Value(obj);
				return operation;
			}
			if(type.value() == OperationType._delete){
				Operation operation = new Operation();
				operation.target = "topic";
				operation.type = OperationType.delete;
				operation.replicated = rep;
				operation.data = new DataObject(CorbaManager.getInstance().createAny());
				operation.data.data.insert_Value(obj);
				return operation;
			}
		}
		else if(target.equals("cache")){
			Operation operation = new Operation();
			operation.target = "cache";
			operation.type = type;
			operation.replicated = rep;
			operation.data = new DataObject(CorbaManager.getInstance().createAny());
			operation.data.data.insert_Value(obj);
			return operation;
		}
		return null;
	}
}
